package com.hq.fxh.Utils;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/30
 * @Email xlwenhao1858@sina.com
 */



public class ScreenUtils {
    /** 屏幕的真实宽度. */
    public static int screenWidth;

    /** 屏幕的真实高度. */
    public static int screenHeight;

    /** UI提供高清图片宽度. */
    public static Integer originalImageWidth = 720;

    /** UI提供高清图片高度. */
    public static Integer originalImageHeight = 1280;

    /** 图片屏幕换算比例_宽度. */
    public static float screenWidthPercentage;//

    /** 图片屏幕换算比例_高度. */
    public static float screenHeightPercentage;

    /** The density. */
    public static float density;

    /** The scale density. */
    public static float scaleDensity;

    /** The xdpi. */
    public static float xdpi;

    /** The ydpi. */
    public static float ydpi;

    /** The density dpi. */
    public static int densityDpi;

    /** The screen min. */
    public static int screenMin; // 宽高中，最小的值

    public static boolean isStatusBar = true;

    private static int currentOrientation;

    /**
     * 初始化屏幕相关参数，放在application里
     *
     * @param context
     * @param originalWidth 设计图宽度
     * @param originalHeight 设计图高度
     */
    public static void getInfo(Context context, Integer originalWidth, Integer originalHeight) {
        originalImageWidth = originalWidth;
        originalImageHeight = originalHeight;
        getInfo(context);
    }

    /**
     * 初始化屏幕相关参数，放在application里
     *
     * @param context
     * @param originalWidth 设计图宽度
     * @param originalHeight 设计图高度
     * @param isMinusStatusBar 是否减去通知栏感度
     */
    public static void getInfo(Context context, Integer originalWidth, Integer originalHeight,
                               boolean isMinusStatusBar) {
        originalImageWidth = originalWidth;
        originalImageHeight = originalHeight;
        isStatusBar = isMinusStatusBar;
        getInfo(context);
    }

    /**
     * Gets the info.
     *
     * @param context the context
     */
    public static void getInfo(Context context) {
        if (null == context)
            return;
        if (originalImageHeight == null || originalImageWidth == null) {
            throw new IllegalArgumentException(
                    "originalImageHeight or originalImageWidth is not null");
        }
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getApplicationContext().getResources().getDisplayMetrics();
        currentOrientation = context.getResources().getConfiguration().orientation;
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        if (isStatusBar) {
            screenHeight = screenHeight - getStatusBarHeight();
        }
        screenWidthPercentage = screenWidth / (originalImageWidth * 1f);
        screenHeightPercentage = screenHeight / (originalImageHeight * 1f);
        screenMin = (screenWidth > screenHeight) ? screenHeight : screenWidth;
        density = dm.density;
        scaleDensity = dm.scaledDensity;
        xdpi = dm.xdpi;
        ydpi = dm.ydpi;
        densityDpi = dm.densityDpi;
    }

    /**
     * 允许的屏幕
     *
     *            Configuration.ORIENTATION_PORTRAIT横屏
     *            Configuration.ORIENTATION_LANDSCAPE竖屏
     * @return true 不同，false相同
     */
    public static boolean isDifferentOrientation(Context context) {
        if (ScreenUtils.screenWidth == 0) {
            return true;
        }
        boolean isDifferent = (currentOrientation == context.getResources().getConfiguration().orientation);
        return !isDifferent;
    }

    /**
     * dip转px.
     *
     * @param dipValue the dip value
     * @return the int
     */
    public static int dip2px(float dipValue) {
        final float scale = ScreenUtils.density;
        return (int)(dipValue * scale + 0.5f);
    }

    /**
     * Gets the status bar height.
     *
     * @return the status bar height
     */
    public static int getStatusBarHeight() {
        return Resources.getSystem().getDimensionPixelSize(
                Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
    }




        /**
         * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
         */
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }

        /**
         * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
         */
        public static int px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / scale + 0.5f);
        }

        /**
         * 将px值转换为sp值，保证文字大小不变
         */
        public static int px2sp(Context context, float pxValue) {
            final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
            return (int) (pxValue / fontScale + 0.5f);
        }

        /**
         * 将sp值转换为px值，保证文字大小不变
         */
        public static int sp2px(Context context, float spValue) {
            final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
            return (int) (spValue * fontScale + 0.5f);
        }



}

